﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _8._1
{
    public partial class MainForm : Form
    {
        private SQLiteConnection connection;
        private string currentEnglish;
        private Random random = new Random();

        public MainForm()
        {
            InitializeComponent();
            InitializeDatabase();
            LoadNextWord();
        }

        private void InitializeDatabase()
        {
            // 创建或连接数据库
            connection = new SQLiteConnection("Data Source=words.db;Version=3;");
            connection.Open();

            // 创建表（如果不存在）
            using (var cmd = new SQLiteCommand(
                "CREATE TABLE IF NOT EXISTS words (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "english TEXT NOT NULL UNIQUE, " +
                "chinese TEXT NOT NULL)", connection))
            {
                cmd.ExecuteNonQuery();
            }

            // 添加示例数据（如果表为空）
            AddSampleData();
        }

        private void AddSampleData()
        {
            // 检查是否已有数据
            using (var cmd = new SQLiteCommand("SELECT COUNT(*) FROM words", connection))
            {
                long count = (long)cmd.ExecuteScalar();
                if (count > 0) return;
            }

            // 添加初始单词
            string[,] sampleWords = {
                {"apple", "苹果"},
                {"book", "书"},
                {"computer", "电脑"},
                {"dictionary", "字典"},
                {"elephant", "大象"},
                {"flower", "花"},
                {"garden", "花园"},
                {"hospital", "医院"}
            };

            for (int i = 0; i < sampleWords.GetLength(0); i++)
            {
                using (var cmd = new SQLiteCommand(
                    "INSERT OR IGNORE INTO words (english, chinese) VALUES (@english, @chinese)",
                    connection))
                {
                    cmd.Parameters.AddWithValue("@english", sampleWords[i, 0]);
                    cmd.Parameters.AddWithValue("@chinese", sampleWords[i, 1]);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        private void LoadNextWord()
        {
            // 随机获取一个单词
            using (var cmd = new SQLiteCommand(
                "SELECT english, chinese FROM words ORDER BY RANDOM() LIMIT 1",
                connection))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        currentEnglish = reader["english"].ToString();
                        lblChinese.Text = reader["chinese"].ToString();
                    }
                }
            }

            // 重置UI状态
            txtInput.Text = "";
            lblResult.Text = "";
            lblResult.ForeColor = SystemColors.ControlText;
            txtInput.Focus();
        }

        private void CheckAnswer()
        {
            string userInput = txtInput.Text.Trim().ToLower();
            string correctAnswer = currentEnglish.ToLower();

            if (userInput == correctAnswer)
            {
                lblResult.Text = "正确 ✓";
                lblResult.ForeColor = Color.Green;
            }
            else
            {
                lblResult.Text = $"错误 ✗ 正确答案: {currentEnglish}";
                lblResult.ForeColor = Color.Red;
            }
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrWhiteSpace(txtInput.Text))
            {
                CheckAnswer();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            LoadNextWord();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            connection?.Close();
        }
    }
}
